<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @framework : Codeigniter Framework
*
* @author: fatangel26
*
*/
	class Cart extends Admin_Controller
	{
		public function __construct()
		{
			parent::__construct();

            if(!$this->my_session->is_Admin())
            {
                redirect(base_url()."backend/auth/login");

                exit();
            }

            $this->load->model("backend/cart_model", "MCart", TRUE);
		}

        public function index()
        {
            $this->_data['title'] = 'Jewel | Danh sách đơn đặt hàng';

            $data['orders'] = $this->MCart->getOrders();

            if(!$data['orders'])
            {
                $data['msg'] = 'Không có dữ liệu để hiển thị.';
            }

            $this->_data['content'] = $this->load->view("backend/cart/order", $data, TRUE);

            $this->load->view("backend/layout", $this->_data);
        }//End function index

        public function updateorder()
        {
            $order_id = $this->input->post('order_id');
            $order_status = $this->input->post('order_status');

            $check = $this->MCart->updateorder($order_id, $order_status);

            $json = array();

            if(!$check)
            {
                $json['error'] = 1;
            }
            else
            {
                $json['error'] = 2;
            }

            echo json_encode($json);
        }

        public function del()
        {
            $order_id = $this->uri->segment(4);

            $check = $this->MCart->delorder($order_id);

            if(!$check)
            {
                $this->native_session->set_flashdata('msg', 'Xoá không thành công');
            }
            else
            {
                $this->native_session->set_flashdata('msg', 'Xoá đơn hàng thành công');
            }

            redirect(base_url()."backend/cart");
        }//End function del

        public function detail()
        {
            $this->_data['title'] = 'Jewel | Chi tiết đơn hàng';

            if(!$this->uri->segment(4) || $this->uri->segment(4) == '')
            {
                show_404();

                exit();
            }

            $order_id = $this->uri->segment(4);

            $data['order'] = $this->MCart->getorder(array('order_id' => $order_id));

            if(!$data['order'])
            {
                $data['msg'] = 'Không tồn tại mã hoá đơn này.';
            }
            else
            {
                $data['order_items'] = $this->MCart->get_order_items($order_id);
            }

            $this->_data['content'] = $this->load->view("backend/cart/detailorder", $data, TRUE);

            $this->load->view("backend/layout", $this->_data);
        }//End function detail

        public function updateship()
        {

            $strUpdate = array(
                'order_id'      => $this->input->post('order_id'),
                'order_ship'    => $this->input->post('order_ship')
            );

            $check = $this->MCart->updateship($strUpdate);

            $json = array();

            if(!$check)
            {
                $json['error'] = 1;
            }
            else
            {
                $json['error'] = 2;
            }

            echo json_encode($json);


        }//End fucntion updateship

        public function export2pdf()
        {
			$this->load->library('mpdf');
            $mpdf = new mPDF('utf-8');

			$mpdf->mirrorMargins = 1;	// Use different Odd/Even headers and footers and mirror margins

			$mpdf->defaultheaderfontsize = 10;	/* in pts */
			$mpdf->defaultheaderfontstyle = B;	/* blank, B, I, or BI */
			$mpdf->defaultheaderline = 1; 	/* 1 to include line below header/above footer */

			$mpdf->defaultfooterfontsize = 12;	/* in pts */
			$mpdf->defaultfooterfontstyle = B;	/* blank, B, I, or BI */
			$mpdf->defaultfooterline = 1; 	/* 1 to include line below header/above footer */


			$mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Hoá đơn');
			$mpdf->SetFooter('{PAGENO}/{nb}|Jewel.com');	/* defines footer for Odd and Even Pages - placed at Outer margin */

			$mpdf->SetFooter(array(
				'L' => array(
					'content' => 'Text to go on the left',
					'font-family' => 'sans-serif',
					'font-style' => 'B',	/* blank, B, I, or BI */
					'font-size' => '10',	/* in pts */
				),
				'C' => array(
					'content' => '- {PAGENO} -',
					'font-family' => 'serif',
					'font-style' => 'BI',
					'font-size' => '18',	/* gives default */
				),
				'R' => array(
					'content' => 'Printed @ {DATE j-m-Y H:m}',
					'font-family' => 'monospace',
					'font-style' => '',
					'font-size' => '10',
				),
				'line' => 1,		/* 1 to include line below header/above footer */
			), 'E'	/* defines footer for Even Pages */
			);

			$order_id = $this->uri->segment(4);

            $data['order'] = $this->MCart->getorder(array('order_id' => $order_id));

            $data['order_items'] = $this->MCart->get_order_items($order_id);

			//echo $this->load->view("backend/cart/printorder",$data,TRUE);

			$mpdf->WriteHTML($this->load->view("backend/cart/printorder",$data,TRUE));
			$mpdf->Output();

        }

	}

/* End of file myfile.php */
/* Location: ./system/modules/mymodule/myfile.php */